<?php
/**
 * Copyright 2011 magdev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * The names of the authors may not be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package      Mashape API-Documentor
 * @author       Marco Graetsch <info@magdev.de>
 * @copyright    2011 Marco Graetsch.development
 * @license      http://opensource.org/licenses/bsd-license.php New BSD License
 * @link         https://code.google.com/p/apidocumentor/
 */

/**
 * CSS parser
 *
 * @author magdev
 */
class Mashdoc_Helper_CssParser
{
    private $_cssUrl = null;
    private $_baseUrl = null;
    private $_useApc = false;
    private $_apcTtl = 3600;
    
    public function __construct($cssUrl = null)
    {
        if (extension_loaded('apc')) {
            $this->_useApc = true;
        }
        if (!is_null($cssUrl)) {
            $this->setCssUrl($cssUrl);
        }
    }
    
    
    public function setCssUrl($cssUrl)
    {
        $this->_cssUrl = $cssUrl;
        $this->_baseUrl = substr($cssUrl, 0, strrpos($cssUrl, '/'));
    }
    
    
    public function parse($force = false)
    {
        $key = sha1($this->_cssUrl);
        if ($this->_useApc && !$force) {
            $success = false;
            $css = apc_fetch($key, &$success);
            if ($success) {
                return $css;
            }
        }
        $css = file_get_contents($this->_cssUrl);
        if (!$css) {
            throw new RuntimeException('No CSS found in '.$this->_cssUrl, Mashdoc::E_NO_STYLESHEET);
        }
        $css = $this->_parseStylesheet($css);
        if ($this->_useApc) {
            apc_store($key, $css, $this->_apcTtl);
        }
        return $css;
    }
    
    
    
    private function _parseStylesheet($css)
    {
        $regexp = '/url\(([^\)]+)\)/msx';
        $result = array();
        if (preg_match_all($regexp, $css, $result)) {
            $files = $result[1];
            $inputs = $result[0];
            $outputs = array();
            
            $dataUri = new Mashdoc_Helper_DataUri();
            foreach ($files as $i => $file) {
                $url = $this->_baseUrl.'/'.$file;
                if ($data = $dataUri->getDataUri($url)) {
                    $outputs[$i] = 'url('.$data.')';
                } else {
                    unset($inputs[$i], $outputs[$i]);
                }
            }
            return str_replace($inputs, $outputs, $css);
        }
        return $css;
    }
}
?>